package com.demo.server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.demo.dao.INewsDao;
import com.demo.entity.News;
import com.demo.entity.base.Page;
import com.demo.web.base.BaseResult;
import com.jweb.annotaion.JAutowired;

public class NewsServer {
	public Map<String, Integer> TYPES = new HashMap<String, Integer>() {
		private static final long serialVersionUID = -1270871085578654146L;
		{
			this.put("支付要闻", 1);
			this.put("分析评论", 2);
			this.put("企业动态", 3);
			this.put("金融支付", 4);
			this.put("海外支付", 5);
			this.put("人物观点", 6);
			this.put("跨境支付", 7);
			this.put("会议活动", 8);
			this.put("贝牛实验室", 9);
			this.put("访谈", 10);
			this.put("精品课程", 11);
			this.put("编辑推荐", 12);
			this.put("附加", 13);
		}
	};

	@JAutowired
	public INewsDao dao;

	public BaseResult<News> listNews(int currentPage, int type) {
		String types = getTypeByID(type);
		Page pg = new Page(currentPage);
		List<News> resultList = dao.listNews(types, pg);
		int allCounts = dao.listNewsCount(types);
		BaseResult<News> basepage = new BaseResult<News>(pg, allCounts);
		basepage.setResultList(resultList);
		return basepage;
	}

	public News getNewsById(int id) {
		return dao.getNewsById(id);
	}

	public List<Map<String, Integer>> listAllType() {
		List<Map<String, Integer>> list = dao.listAllType();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Integer> map = list.get(i);
			map.put("id", TYPES.get(map.get("type")));
			list.set(i, map);
		}
		return list;
	}

	public String getTypeByID(int type) {
		String types = null;
		for (Entry<String, Integer> entry : TYPES.entrySet()) {
			if (entry.getValue() == type) {
				types = entry.getKey();
			}
		}
		return types;
	}

	public List<Map<Integer, String>> listHot48() {
		return dao.listHot48();
	}

	public List<Map<Integer, String>> listHot7Day() {
		return dao.listHot7Day();
	}

	public List<Map<Integer, String>> listHotMonth() {
		return dao.listHotMonth();
	}

	public List<Map<Integer, String>> listHotComment() {
		return dao.listHotComment();
	}

}
